<?php 
namespace App\Services\User;
use App\Facades\Infra\User\DoctorManage;
use App\Facades\Infra\User\UserManage;
use App\Facades\Services\User\UserService;

class DoctorService{

	public function judge($data){

		$conditions['id'] = $data['id'];

		$doctor = DoctorManage::getDoctor($conditions)->first();

		// dd($doctor->wechatDoctor->openid);

		if($doctor['dr_type'] == 1){
			return true;
		}

		$formOpenId = UserService::getUser()->form_openid;


		if($formOpenId  == $doctor->wechatDoctor->openid){
			return true;
		}

		return false;
	}

	public function getDoctorAndMessage($data){

		$conditions['id'] = $data['id'];

		return DoctorManage::getDoctorAndMessage($conditions);
	}

	public function getDoctorsByWaiKe(){

		$conditions['dr_type'] = 1;

		return $this->getDoctorsByConditions($conditions);
	}

	public function getDoctorsByNeiKe(){
		$conditions['dr_type'] = 2;

		return $this->getDoctorsByConditions($conditions);
	}

	public function getDoctorByOneSelf(){
		$openid = session('wechat.oauth_user')->getId();

		$conditions['openid'] = $openid;

		$formOpenId = UserManage::getUser($conditions)->first()->form_openid;

		$conditionsDoctor['openid'] = $formOpenId;

		$oneSelf = DoctorManage::getDoctorInfosByWith($conditionsDoctor);

		if($oneSelf == null){
			return $oneSelf;
		}

		return DoctorManage::getDoctorInfosByWith($conditionsDoctor)->toArray()['wechat_doctor_info'];
	}

	private function getDoctorsByConditions($conditions){

		$doctors = DoctorManage::getDoctorInfos($conditions);

		if($doctors != null){
			return $doctors->toArray();
		}

		return $doctors;
	}
}